<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>RabbitMQ 交换机类型深度解析</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .card-hover {
            transition: all 0.3s ease;
            border: 1px solid rgba(255, 255, 255, 0.1);
            backdrop-filter: blur(10px);
        }
        
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            border-color: rgba(102, 126, 234, 0.5);
        }
        
        .gradient-text {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        
        .code-block {
            background: #1e1e1e;
            border-radius: 8px;
            overflow-x: auto;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }
        
        .code-block pre {
            margin: 0;
            padding: 1.5rem;
            color: #d4d4d4;
            font-family: 'Consolas', 'Monaco', monospace;
            font-size: 14px;
            line-height: 1.6;
        }
        
        .section-divider {
            height: 2px;
            background: linear-gradient(to right, transparent, #667eea, transparent);
            margin: 3rem 0;
        }
        
        .feature-icon {
            width: 60px;
            height: 60px;
            display: flex;
            align-items: center;
            justify-content: center;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 50%;
            color: white;
            font-size: 24px;
            margin-bottom: 1rem;
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 3rem;
            padding-right: 0.5rem;
            margin-top: 0.5rem;
            font-weight: 700;
            color: #667eea;
        }
        
        .highlight-box {
            background: linear-gradient(135deg, rgba(102, 126, 234, 0.1) 0%, rgba(118, 75, 162, 0.1) 100%);
            border-left: 4px solid #667eea;
            padding: 1.5rem;
            margin: 2rem 0;
            border-radius: 0 8px 8px 0;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6">
            <div class="text-center">
                <h1 class="text-5xl md:text-6xl font-bold mb-6">
                    RabbitMQ 交换机类型
                    <span class="block text-3xl md:text-4xl mt-4 font-light opacity-90">深度解析与实践指南</span>
                </h1>
                <p class="text-xl md:text-2xl max-w-4xl mx-auto leading-relaxed opacity-95">
                    探索分布式消息队列的核心组件，掌握四种交换机类型的精髓，构建高效可靠的消息传递系统
                </p>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto px-6 py-12 max-w-6xl">
        <!-- Introduction -->
        <section class="bg-white rounded-2xl shadow-xl p-8 md:p-12 mb-12">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap">在</span>分布式消息队列系统中，交换机负责将消息从生产者正确地路由到相应的队列中。在RabbitMQ中，交换机有多种类型，每种类型都有其独特的功能和应用场景。理解这些交换机的工作原理和使用方法，是我们构建高效、可靠的消息传递系统的基础。
            </p>
            
            <div class="mermaid mt-8">
                <script type="text/javascript">
                    mermaid.initialize({ theme: 'default' });
                </script>
                graph LR
                    A[生产者] -->|发送消息| B[交换机]
                    B -->|路由规则| C[队列1]
                    B -->|路由规则| D[队列2]
                    B -->|路由规则| E[队列3]
                    C --> F[消费者1]
                    D --> G[消费者2]
                    E --> H[消费者3]
                    
                    style A fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                    style B fill:#764ba2,stroke:#fff,stroke-width:2px,color:#fff
                    style C fill:#f093fb,stroke:#fff,stroke-width:2px,color:#fff
                    style D fill:#f093fb,stroke:#fff,stroke-width:2px,color:#fff
                    style E fill:#f093fb,stroke:#fff,stroke-width:2px,color:#fff
                </script>
            </div>
        </section>

        <!-- Exchange Role Section -->
        <section class="mb-12">
            <h2 class="text-3xl font-bold mb-8 gradient-text">交换机在消息路由中的作用</h2>
            
            <div class="grid md:grid-cols-2 gap-6">
                <div class="bg-white rounded-xl p-6 card-hover">
                    <div class="feature-icon">
                        <i class="fas fa-route"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3">路由机制的核心</h3>
                    <p class="text-gray-700">交换机是RabbitMQ消息路由机制的核心，负责将消息从生产者正确地路由到消费者。生产者将消息发送到交换机，而不是直接发送到队列。</p>
                </div>
                
                <div class="bg-white rounded-xl p-6 card-hover">
                    <div class="feature-icon">
                        <i class="fas fa-random"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3">灵活的路由策略</h3>
                    <p class="text-gray-700">不同类型的交换机提供了不同的路由策略，满足多样化的消息路由需求，从精确匹配到模式匹配，从广播到基于属性的路由。</p>
                </div>
                
                <div class="bg-white rounded-xl p-6 card-hover">
                    <div class="feature-icon">
                        <i class="fas fa-unlink"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3">解耦生产者和消费者</h3>
                    <p class="text-gray-700">交换机在生产者和消费者之间起到了解耦的作用，提高了系统的灵活性和可维护性，使得生产者和消费者可以独立演化和扩展。</p>
                </div>
                
                <div class="bg-white rounded-xl p-6 card-hover">
                    <div class="feature-icon">
                        <i class="fas fa-shield-alt"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3">高可用性和安全性</h3>
                    <p class="text-gray-700">通过合理配置交换机和队列绑定，可以实现消息的高可用性和负载均衡，结合权限管理机制，加强消息系统的安全性。</p>
                </div>
            </div>
        </section>

        <div class="section-divider"></div>

        <!-- Exchange Types Overview -->
        <section class="mb-12">
            <h2 class="text-3xl font-bold mb-8 gradient-text">交换机的主要类型</h2>
            
            <div class="bg-white rounded-2xl shadow-xl p-8 mb-8">
                <p class="text-lg text-gray-700 mb-6">
                    RabbitMQ中有四种主要类型的交换机，每种类型都有其独特的路由机制和应用场景。选择合适的交换机类型是构建高效消息系统的关键。
                </p>
                
                <div class="mermaid">
                    <script type="text/javascript">
                        mermaid.initialize({ theme: 'default' });
                    </script>
                    graph TD
                        A[Exchange Types] --> B[Direct Exchange]
                        A --> C[Fanout Exchange]
                        A --> D[Topic Exchange]
                        A --> E[Headers Exchange]
                        
                        B --> F[精确匹配路由键]
                        C --> G[广播到所有队列]
                        D --> H[模式匹配路由]
                        E --> I[基于消息头属性]
                        
                        style A fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                        style B fill:#f093fb,stroke:#fff,stroke-width:2px
                        style C fill:#f093fb,stroke:#fff,stroke-width:2px
                        style D fill:#f093fb,stroke:#fff,stroke-width:2px
                        style E fill:#f093fb,stroke:#fff,stroke-width:2px
                </script>
                </div>
            </div>
        </section>

        <!-- Direct Exchange -->
        <section class="mb-12">
            <div class="bg-gradient-to-r from-purple-50 to-pink-50 rounded-2xl p-8 mb-8">
                <h2 class="text-3xl font-bold mb-6 flex items-center">
                    <i class="fas fa-bullseye mr-4 text-purple-600"></i>
                    Direct Exchange
                </h2>
                
                <p class="text-lg text-gray-700 mb-6">
                    Direct Exchange 是最基本的交换机类型，通过精确匹配路由键将消息路由到相应的队列。消息的路由键必须与队列的绑定键完全匹配。
                </p>
                
                <div class="highlight-box">
                    <h4 class="font-semibold text-lg mb-2">核心特点</h4>
                    <ul class="space-y-2 text-gray-700">
                        <li><i class="fas fa-check-circle text-green-500 mr-2"></i>精确匹配：路由键必须完全匹配</li>
                        <li><i class="fas fa-check-circle text-green-500 mr-2"></i>简单易用：配置直观，易于理解</li>
                        <li><i class="fas fa-check-circle text-green-500 mr-2"></i>精确控制：对消息流向的精确控制</li>
                    </ul>
                </div>
                
                <div class="code-block mt-6">
                    <pre><code>// 声明 Direct Exchange
channel.exchangeDeclare("direct_exchange", BuiltinExchangeType.DIRECT);

// 声明队列并绑定
channel.queueDeclare("queue1", true, false, false, null);
channel.queueBind("queue1", "direct_exchange", "routing_key1");

// 发送消息
String message = "Hello World!";
channel.basicPublish("direct_exchange", "routing_key1", null, message.getBytes());</code></pre>
                </div>
                
                <div class="mt-6 grid md:grid-cols-3 gap-4">
                    <div class="bg-white rounded-lg p-4 text-center">
                        <i class="fas fa-tasks text-3xl text-purple-600 mb-2"></i>
                        <h5 class="font-semibold">任务分发系统</h5>
                    </div>
                    <div class="bg-white rounded-lg p-4 text-center">
                        <i class="fas fa-user-friends text-3xl text-purple-600 mb-2"></i>
                        <h5 class="font-semibold">点对点通信</h5>
                    </div>
                    <div class="bg-white rounded-lg p-4 text-center">
                        <i class="fas fa-file-alt text-3xl text-purple-600 mb-2"></i>
                        <h5 class="font-semibold">日志系统</h5>
                    </div>
                </div>
            </div>
        </section>

        <!-- Fanout Exchange -->
        <section class="mb-12">
            <div class="bg-gradient-to-r from-blue-50 to-cyan-50 rounded-2xl p-8 mb-8">
                <h2 class="text-3xl font-bold mb-6 flex items-center">
                    <i class="fas fa-broadcast-tower mr-4 text-blue-600"></i>
                    Fanout Exchange
                </h2>